import { Node, UITransform, Vec3, Widget } from 'cc';
import { app } from 'db://app-game/core/app';
import { ViewUtil } from 'db://app-game/core/utils/ViewUtil';
import { ecs } from 'db://app-game/libs/ecs/ECS';
import { GameEvent } from '../../../../common/config/GameEvent';
import { GamePrefab } from '../../../../common/config/GamePrefab';
import { Joysitick } from '../../../biz/joysitick/Joysitick';
import { LevelMap } from '../../../biz/level-map/LevelMap';
import { SkillButton } from '../../../biz/skill-button/SkillButton';
import { SkillButtonLoadParams } from '../../../biz/skill-button/SkillButton.type';
import { LevelLayoutData } from '../../../biz/storage/LocalStorageManager.type';
import { smc } from '../../../singleton/SingletonModuleComp';
import { Level } from '../Level';
import { LevelModelComp } from '../model/LevelModelComp';

/** 关卡基础数据 */
@ecs.register('LevelLayout')
export class LevelLayoutComp extends ecs.Comp {
  reset(entity: ecs.Entity): void {}
}

/** 关卡界面布局系统 */
@ecs.register('LevelLayout')
export class LevelLayoutSystem
  extends ecs.ComblockSystem
  implements ecs.IEntityEnterSystem, ecs.ISystemUpdate
{
  filter(): ecs.IMatcher {
    return ecs.allOf(LevelModelComp, LevelLayoutComp);
  }

  entityEnter(level: Level): void {
    // 1.UI层渲染
    this.layerRender(level);
  }
  update(level: Level): void {
    if (!level.LevelMap.LevelMapModelRender.onRenderFinish) return;
    smc.account.AccountModel.role.addRandomMove();
    // 2.通知渲染完成,发起切换场景
    app.message.dispatchEvent(GameEvent.SceneChangeComplete);
    // 3.删除加载组件
    level.remove(LevelLayoutComp);
  }

  /** 关卡布局渲染 */
  layerRender(level: Level) {
    const { mapId, roleInitPosition } = level.LevelModel;
    // 1.渲染地图布局
    this.levelMapRender(level, mapId);
    // 1.渲染主角
    this.levelRoleRender(level, roleInitPosition);
    // 2.UI层渲染
    this.levelUIRender(level);
  }

  /** 关卡地图渲染 */
  levelMapRender(level: Level, mapId: number) {
    level.LevelMap = ecs.getEntity<LevelMap>(LevelMap);
    level.LevelMap.LevelMapModel.id = mapId;
    level.LevelMap.load();
  }

  /** 关卡角色渲染 */
  levelRoleRender(level: Level, pos: Vec3) {
    const role = smc.account.AccountModel.role;
    // 1.渲染主角
    role.load(level.LevelMap.roleFactoryNode, pos);
    const roleNode = role.RoleView.node;

    // 2.地图渲染跟随主角
    level.GameCamera.setfollowTarget(roleNode);
    level.LevelMap.setfollowTarget(roleNode);
  }

  /** 关卡UI渲染 */
  levelUIRender(level: Level) {
    // 1.创建ui内容节点
    const uiNode = this.createUiNode();
    level.LevelModel.uiNode = uiNode;

    // 2.获取关卡UI渲染位置
    const llt = smc.storageData.gameConfig.levelLayout;
    const lld = llt[llt.current];

    // 3.创建角色状态面板
    this.createRoleStatusPanel(uiNode);
    // 4.创建摇杆
    this.createJoysitick(uiNode, lld);
    // 5.创建技能按钮
    this.createRoleSkillButton(uiNode, lld);
  }

  /** 创建角色状态面板 */
  createRoleStatusPanel(uiNode: Node) {
    const node = ViewUtil.createPrefabNode(GamePrefab.RoleBaseState);
    const widget = node.getComponent(Widget);
    widget.isAlignLeft = true;
    widget.isAlignTop = true;
    widget.left = 0;
    widget.top = 0;
    node.parent = uiNode;
  }

  /** 创建摇杆 */
  createJoysitick(uiNode: Node, lld: LevelLayoutData) {
    const role = smc.account.AccountModel.role;
    // 1.创建移动摇杆
    const mr = {
      id: 0,
      parent: uiNode,
      width: lld.moveJoysitick.width,
      height: lld.moveJoysitick.height,
      absolutePosition: {
        left: lld.moveJoysitick.left,
        top: lld.moveJoysitick.top,
      },
    };
    const mj = ecs.getEntity<Joysitick>(Joysitick);
    mj.load(mr);

    // 2.创建攻击摇杆
    const ar = {
      id: role.RoleModelEquipment.weapon.joysitickId,
      parent: uiNode,
      width: lld.attackJoysitick.width,
      height: lld.attackJoysitick.height,
      absolutePosition: {
        left: lld.attackJoysitick.left,
        top: lld.attackJoysitick.top,
      },
    };
    const aj = ecs.getEntity<Joysitick>(Joysitick);
    aj.load(ar);

    //3.给角色赋值摇杆
    role.addRoleControl({ moveJoysitick: mj, attackJoysitick: aj });
  }

  /** 创建角色携带的技能按钮 */
  createRoleSkillButton(uiNode: Node, lld: LevelLayoutData) {
    const { equipSkills } = smc.storageData.roles[smc.storageData.activeRole];
    const { skills } = equipSkills[equipSkills.current];
    Object.keys(skills).forEach(key => {
      const skill = skills[key];
      const skillButton = ecs.getEntity<SkillButton>(SkillButton);
      const params: SkillButtonLoadParams = {
        id: skill.id,
        level: skill.level,
        parent: uiNode,
        size: lld[key].size,
        absolutePos: { left: lld[key].left, top: lld[key].top },
      };
      skillButton.load(params);
    });
  }

  /** 创建切换武器栏 */

  /** 创建ui节点 */
  createUiNode() {
    const uiNode = new Node();
    uiNode.name = 'ui-content';
    uiNode.layer = 1;
    uiNode.addComponent(UITransform);
    const uiWidget = uiNode.addComponent(Widget);
    uiWidget.isAlignLeft = true;
    uiWidget.isAlignRight = true;
    uiWidget.isAlignTop = true;
    uiWidget.isAlignBottom = true;
    uiWidget.left = 20;
    uiWidget.right = 20;
    uiWidget.top = 20;
    uiWidget.bottom = 20;
    uiWidget.alignMode = 1;
    return uiNode;
  }
}
